/**
 * Created by lizhe on 2018/4/16.
 */
var zTree;
var setting = {
    callback: {
        onClick: zTreeOnClick
    },
    data: {
        simpleData: {
            enable: true
        }
    }
};

var settingAdd = {
    callback: {
        onClick: zTreeOnClickAdd
    },
    data: {
        simpleData: {
            enable: true
        }
    }
};

var Init = [];
var ZtreeId = 0;
var ZtreeName = "";
var addZtreeId = 0;
var addZtreeName = "";
var editZtreeId = 0;
var editZtreeName = "";
var type =["","文章","图片","教师","学生","基本概况","教师风采"];

var zNodes =[];
var zNodes2 = [];

var ae = 0;

$(document).ready(function(){
    //获取栏目列表
    queryColumn();

    //新增
    $("#add_column").click(function(){
        document.getElementById("add_type_1").checked = true;
        document.getElementById("add_name").value = "";
        document.getElementById("add_belong_ID").innerHTML = "";
        document.getElementById("add_belong_Name").value = "";
        document.getElementById("add_num").value = "0";
        ae = 1;
    });

    //确认新增
    $("#add_confirm").click(function(){
        var t = document.getElementById("add_type").getElementsByTagName("input");
        var icon = document.getElementById("add_icon").name;
        var type;
        for(var i = 0;i< t.length;i++){
            if(t[i].checked == true){
                var str = t[i].id;
                str = str.split("_");
                type = str[2];
                break;
            }
        }
        var title = document.getElementById("add_name").value;
        var parentId = document.getElementById("add_belong_ID").innerHTML;
        var priority = document.getElementById("add_num").value;
        if(title==""){
            modalinfo("栏目名称不能为空");
        }else if(parentId==""){
            modalinfo("请选择栏目所属");
        }else{
            var data={
                title:title,
                type:type,
                parentId:parentId,
                priority:priority,
                icon:icon
            };
            $.ajax({
                url : urladdr + "/web/menu/add?token="+Mytoken,
                method : "POST",
                contentType:"application/json; charset=utf-8",
                data : JSON.stringify(data),
                success : function(data){
                    if(data.code == 0){
                        modalinfo("新增成功");
                        $("#toColumn").click();
                        queryColumn();
                    }else if(data.code == 401){
                        location.href = "index.html"
                    }else{
                        modalinfo(data.msg);
                    }
                }
            });
        }
    });


    //修改
    $("#edit_column").click(function(){
        if(ZtreeId == 0){
            modalinfo("请选择要修改的栏目");
        }else{
            $("#toEdit").click();
            ae = 2;
            var s = 0;
            var parentName = "";
            for(var i=0;i<Init.length;i++){
                if(ZtreeId==Init[i].id){
                    s = i;
                    break;
                }
            }
            for(var j = 0;j<Init.length;j++){
                if(Init[s].parentId == Init[j].id){
                    parentName = Init[j].title;
                    break;
                }
            }
            document.getElementById("edit_type_"+Init[s].type).checked = true;
            document.getElementById("edit_name").value = Init[s].title;
            document.getElementById("edit_belong_ID").innerHTML = Init[s].parentId;
            var icon = Init[s].icon;
            if(icon == null){
                document.getElementById("edit_icon").src = 'images/9_02.png';
                document.getElementById("edit_icon").name = 'images/9_02.png';
            }else{
                document.getElementById("edit_icon").src = Init[s].icon;
                document.getElementById("edit_icon").name = Init[s].icon;
            }
            if(Init[s].parentId=="0"){
                document.getElementById("edit_belong_Name").value = "根目录";
            }else{
                document.getElementById("edit_belong_Name").value = parentName;
            }
            document.getElementById("edit_num").value = Init[s].priority;
            document.getElementById("edit_id").innerHTML = Init[s].id;
            document.getElementById("edit_height").innerHTML = Init[s].height;
        }
    });

    //修改确定
    $("#edit_confirm").click(function(){
        var t = document.getElementById("edit_type").getElementsByTagName("input");
        var id = document.getElementById("edit_id").innerHTML;
        var height = document.getElementById("edit_height").innerHTML;
        var icon = document.getElementById("edit_icon").name;
        var type;
        for(var i = 0;i< t.length;i++){
            if(t[i].checked == true){
                var str = t[i].id;
                str = str.split("_");
                type = str[2];
                break;
            }
        }
        var title = document.getElementById("edit_name").value;
        var parentId = document.getElementById("edit_belong_ID").innerHTML;
        var priority = document.getElementById("edit_num").value;
        if(title==""){
            modalinfo("栏目名称不能为空");
        }else if(parentId==""){
            modalinfo("请选择栏目所属");
        }else{
            var data={
                id:id,
                height:height,
                title:title,
                type:type,
                parentId:parentId,
                priority:priority,
                icon:icon
            };
            $.ajax({
                url : urladdr + "/web/menu/update?token="+Mytoken,
                method : "POST",
                contentType:"application/json; charset=utf-8",
                data : JSON.stringify(data),
                success : function(data){
                    if(data.code == 0){
                        modalinfo("编辑成功");
                        $("#toColumn").click();
                        queryColumn();
                    }else if(data.code == 401){
                        location.href = "index.html"
                    }else{
                        modalinfo(data.msg);
                    }
                }
            });
        }
    });

    //删除
    $("#delete_column").click(function(){
        var have = 0;
        if(ZtreeId == 0){
            modalinfo("请选择要删除的栏目");
        }else{
            showmodal("#deletemodal");
            for(var i = 0;i<Init.length;i++){
                if(ZtreeId == Init[i].parentId){
                    have = 1;
                    break;
                }
            }
        }
        document.getElementById("del_id").innerHTML = ZtreeId;
        if(have == 0){
            document.getElementById("del_info").innerHTML = "确定要删除选中的记录？"
        }else{
            document.getElementById("del_info").innerHTML = "该目录存在子节点，是否确认删除？"
        }
    });

    //确认删除
    $("#del_confirm").click(function(){
        var id = document.getElementById("del_id").innerHTML;
        var data={
            token:Mytoken,
            id:id
        };
        $.ajax({
            url : urladdr + "/web/menu/delete",
            method : "POST",
            data : data,
            success : function(data){
                if(data.code == 0){
                    modalinfo("删除成功");
                    modalclose("#deletemodal");
                    queryColumn();
                }else if(data.code == 401){
                    location.href = "index.html"
                }else{
                    modalinfo(data.msg);
                    modalclose("#deletemodal");
                }
            }
        });
    });

    //返回
    $(".btn-warning").click(function(){
       $("#toColumn").click();
    });

    $("#column_confirm").click(function(){
        if(addZtreeName==""){
            modalinfo("请选择栏目所属");
        }else{
            if(ae == 1){
                document.getElementById("add_belong_ID").innerHTML = addZtreeId;
                document.getElementById("add_belong_Name").value = addZtreeName;
                modalclose("#columnmodal");
            }else if(ae == 2) {
                document.getElementById("edit_belong_ID").innerHTML = addZtreeId;
                document.getElementById("edit_belong_Name").value = addZtreeName;
                modalclose("#columnmodal");
            }
        }
    });

    //选择icon
    $(".iconImg").click(function(){
        var parent = this.parentElement;
        var objs = parent.getElementsByTagName("img");
        for(var i = 0;i<objs.length;i++){
            objs[i].style.border = "none";
        }
        this.style.border = "1px solid #00B83F"
    });
    //确认icon
    $("#icon_confirm").click(function(){
        var x = 0;
        var obj = document.getElementById("iconList").getElementsByTagName("img");
        for(var i = 0;i<obj.length;i++){
            if(obj[i].style.border == "1px solid rgb(0, 184, 63)"){
                x = 1;
                var icon = obj[i].name;
                document.getElementById("add_icon").src = icon;
                document.getElementById("add_icon").name = icon;

                document.getElementById("edit_icon").src = icon;
                document.getElementById("edit_icon").name = icon;
                modalclose("#iconmodal");
                break;
            }
        }
        if(x!=1){
            modalinfo("请选择图标");
        }
    });
});

//ztree回调
function zTreeOnClick(event, treeId, treeNode) {
    var id = treeNode.id;
    var name = treeNode.name;
    var tId = treeNode.tId;
    ZtreeId = id;
    ZtreeName = name;
}

//ztreeAdd回调
function zTreeOnClickAdd(event, treeId, treeNode) {
    var id = treeNode.id;
    var name = treeNode.name;
    var tId = treeNode.tId;
    addZtreeId = id;
    addZtreeName = name;
}

function queryColumn(){
    $.ajax({
        url : urladdr + "/web/menu/list",
        method : "GET",
        data : {
            token:Mytoken
        },
        success : function(data){
            if(data.code == 0){
                Init.splice(0,Init.length);
                zNodes.splice(0,zNodes.length);
                $.each(data.data,function(i,date){
                    if(date.height == 0){
                        var d={
                            id:date.id, pId:date.parentId, name:date.title+"("+type[date.type]+")", open:true, iconSkin:"pIcon01"
                        };
                    }else if(date.height == 1){
                        d={
                            id:date.id, pId:date.parentId, name:date.title+"("+type[date.type]+")", open:true, iconSkin:"icon01"
                        };
                    }else if(date.height == 2){
                        d={
                            id:date.id, pId:date.parentId, name:date.title+"("+type[date.type]+")", iconSkin:"icon03"
                        };
                    }
                    zNodes.push(d);

                    var da = {
                        id:date.id,parentId:date.parentId,title:date.title,type:date.type,height:date.height,priority:date.priority,icon:date.icon
                    };
                    Init.push(da);
                });
                setTimeout(function(){
                    $.fn.zTree.init($("#tree"), setting, zNodes);
                },100)
            }else if(data.code == 401){
                location.href = "index.html"
            }else{
                modalinfo(data.msg);
            }
        }
    });
}


function selectTree(){
    $.ajax({
        url : urladdr + "/web/menu/list",
        method : "GET",
        data : {
            token:Mytoken
        },
        success : function(data){
            if(data.code == 0){
                zNodes2.splice(0,zNodes2.length);
                var init = {
                    id:0, pId:0, name:"根目录",open:true,iconSkin:"pIcon01"
                };
                zNodes2.push(init);
                $.each(data.data,function(i,date){
                    if(date.height == 0){
                        var d={
                            id:date.id, pId:date.parentId, name:date.title, open:true, iconSkin:"pIcon01"
                        };
                        zNodes2.push(d);
                    }else if(date.height == 1) {
                        d = {
                            id: date.id, pId: date.parentId, name: date.title, open: true, iconSkin: "icon01"
                        };
                        zNodes2.push(d);
                    }
                });
                setTimeout(function(){
                    $.fn.zTree.init($("#selectTree"), settingAdd, zNodes2);
                },100)
            }else if(data.code == 401){
                location.href = "index.html"
            }else{
                modalinfo(data.msg);
            }
        }
    });
}

function selectBelong(){
    document.getElementById("add_belong_Name").blur();
    showmodal("#columnmodal");
    selectTree();
}

function selectIcon(){
    showmodal('#iconmodal');
}
